
<?php
$task_name=replatestring($_POST['task_name'],false);
$contract_start=$_POST['contract_start'];
$start_time=strtotime($contract_start);
$contract_end=$_POST['contract_end'];
$end_time=strtotime($contract_end);
$mucdo=$_POST['mucdo'];
$brief=$_POST['brief'];
$id=replatestring($_POST[id],false);
$sender=$_SESSION['name'];
$thoigian=time();
$user=$_SESSION['userid'];
$room=$_SESSION['room'];
$count=$_POST[count];
$nguoinhan=$_POST['nguoitiepnhan'];
$nguoitheodoi=$_POST['nguoitheodoi'];
//lay email nguoi nhan
$sqlmail=mysql_query("select 
					tb_account_admin.account_id,
					tb_account_admin.account_email
				from 
					tb_account_admin
			where tb_account_admin.account_id='$nguoinhan'
			");
	$rsmail=mysql_fetch_array($sqlmail);
	$email_nguoinhan=$rsmail['account_email'];

//$nguoitheodoi=$_POST['nguoitheodoi'];
 $Nguoi_Quan_Ly_Khac=explode(',',$_POST['Nguoi_Quan_Ly_Khac']);
 $songuoi=count($Nguoi_Quan_Ly_Khac)-1;

//$hinhanhsp=$_POST['sanpham_hinhanh'];
//Kiem tra tính hợp lệ của dữ liệu
$soanh=$_POST['hdnsoanh'];
$hinhanhsp = "";
for($i=1;$i<=$soanh;$i++){
	if($i<$soanh)$hinhanhsp .= $_POST["sanpham_hinhanh".$i]."~";
	else $hinhanhsp .= $_POST["sanpham_hinhanh".$i];
}


if(isset($_POST['submit'])) {
	
		if($_SESSION['level']==2 or $_SESSION['level']==3)
		{
			
				mysql_query("
				insert into tb_task (task_name,contract_start,contract_end,level,brief,file_attach,sender,nguoinhan,nguoitheodoi,date_creative,status,room_id)     	                         		value('$task_name','$start_time','$end_time','$mucdo','$brief','$hinhanhsp','$sender','$nguoinhan','$nguoitheodoi','$thoigian',0,'$room'
				)");
			 $congviec_id=mysql_insert_id();
			 mysql_query("insert into chuyentiep(congviec_id,nguoinhan_id,nguoichuyen,nguoichuyen_id,time,trangthai) 
			 				value('$congviec_id','$nguoinhan','$sender','$user',$thoigian,0)
			 						
			 	");
		for($n=1;$n<=$songuoi;$n++)
				{
					//echo $n;
					 $tongnguoitheodoi=$Nguoi_Quan_Ly_Khac[$n-1].'<br>';
				
				mysql_query("insert into nguoitheodoi(nguoitheodoi_id,congviec_id)
									value('$tongnguoitheodoi','$congviec_id')
				");
				}
			//gui mail	
			require("mailer/class.phpmailer.php");
				$mail = new PHPMailer();
				//********************************************/
				$SMTP_Host = "thietkewebsitetop.com";
				$SMTP_Port = 25;
				$SMTP_UserName = "noreply@thietkewebsitetop.com";
				$SMTP_Password = "Tn0466800001";
				$from = $SMTP_UserName;
				$to = $email_nguoinhan;
				//$cc='tu@tamnguyen.vn';
				
				// Luu y: $SMTP_UserName = $from
				//********************************************/
				
				$mail->IsSMTP();
				$mail->Host     = $SMTP_Host;
				$mail->SMTPAuth = true;
				$mail->Username = $SMTP_UserName;
				$mail->Password = $SMTP_Password;
				
				$mail->From     = $from;
				$mail->FromName = $sender;
				$mail->CharSet ="utf-8";
				$mail->AddAddress($to);
				//$mail->AddCC($cc);
				//$mail->AddBCC($bc);
				//$mail->AddReplyTo("info@site.com","Information");
				
				$mail->WordWrap = 50;
				//$mail->AddAttachment("/var/tmp/file.tar.gz");
				//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
				$mail->IsHTML(true);
				
				$mail->Subject  =  'Bạn đã được giao công việc mới từ http://noibo.tamnguyen.vn';
				$mail->Body     =  '<b>Yêu cầu: <b>'.$brief.'<br>
									<b>Người giao việc: <b>'.$sender.'
									
									';
				$mail->AltBody  =  "";
				
				if(!$mail->Send())
				{
				   echo "Mail gui khong thanh cong! <p>";
				   echo "Thong bao loi: " . $mail->ErrorInfo;
				   exit;
				}
			
			$cauthongbao="Yêu cầu của bạn đã gửi thành công";
			$link="index.php";
			thongbao($cauthongbao,$link);
			
		}
		
		
}
else{
		$cauthongbao="Dữ liệu không hợp lệ";
		$link="index.php?tn=add_task";
		thongbao($cauthongbao,$link);
}
?>